﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;


namespace WebSite.UserSetting
{
    public partial class Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadContent();
                BindUserIndexNews();
            }
        }

        #region 加载用户信息
        private void LoadContent()
        {
            Users uinfo = UserBase.GetCurrentUser();
            //判断该用户是否填写了个性域名
            string realmname = uinfo.RealmName;
            if (string.IsNullOrEmpty(realmname))
            {
                Response.Redirect("../UserSetting/RealmNameSet.aspx");
                return;
            }
            UsersHelper uhelper = new UsersHelper();
            //账户总收入
            float totalmonty = uhelper.GetTotalMoney(uinfo.ID);
            litTotalMoney.Text = totalmonty.ToString();
            //账户目前金额
            float currentmoney = uhelper.GetCurrentMoney(uinfo.ID);
            lblCurrentMoney.Text = currentmoney.ToString();
            //账户可提现金额
            float cashmoney = uhelper.GetCashMoney(uinfo.ID);
            lblCashMoney.Text = cashmoney.ToString();

            //被关注人数
            litBeFriendNum.Text = uinfo.ToBeFriendNum.ToString();

            //用户广告位数
            string adposition = "";
            UserAdPositionHelper uaphelper = new UserAdPositionHelper();
            Criteria c = new Criteria();
            c.Add(CriteriaType.Equals, "UsersID", uinfo.ID);
            List<UserAdPosition> uaplist = uaphelper.QueryUserAdPositionBySearch(c, null);
            if (uaplist != null && uaplist.Count > 0)//有广告位
            {
                int totalnum = uaplist.Count;//总的广告位数
                int guoqinum = 0;
                for (int i = 0; i < uaplist.Count; i++)
                {
                    DateTime dtend = uaplist[i].EndDate;//广告位的结束日期
                    if (DateTime.Now.AddDays(7) > dtend)
                    {
                        guoqinum++;
                    }
                }
                adposition = "<a href=\"../UserAd/MyAdPosition.aspx\">您当前有<font color=\"red\">" + totalnum + "</font>个属于自己的广告位，<font color=\"red\">" + guoqinum + "</font>个即将过期，立即查看</a>";
            }
            else//没有广告位
            {
                adposition = "<a href=\"../UserAd/MyAdPosition.aspx\">您还没有属于自己的广告位，马上购买</a>";
            }
            litAdPosition.Text = adposition;

            //加载当前用户的权限和最低取现金额
            string roleid = uinfo.RoleID;
            Role rinfo = new Role();
            RoleHelper rhelper = new RoleHelper();
            rinfo = rhelper.GetRole(roleid);
            string rolename = rinfo.RoleName;
            litRoleName.Text = rolename;
            litBlogPVNum.Text = uinfo.BlogPVNum.ToString();
        }
        #endregion

        #region 加载系统用户首页信息
        private void BindUserIndexNews()
        {
            News ninfo = new News();
            NewsHelper nhelper = new NewsHelper();
            Criteria c = new Criteria();
            c.Add(CriteriaType.Equals, "NewsTypeID", "userindex");
            c.Add(CriteriaType.Equals, "Flag", 0);
            List<News> nlist = nhelper.QueryNewsBySearch(c, null);
            if (nlist != null && nlist.Count > 0)
            {
                ninfo = nlist[0];
                litUserIndex.Text = ninfo.NewsContent;
            }
        }
        #endregion
    }
}